package com.xuexiang.xui_lib.component.imageview.imageload;

import ohos.agp.components.AttrHelper;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.app.dispatcher.TaskDispatcher;
import ohos.app.dispatcher.task.TaskPriority;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_PARENT;
import static ohos.agp.components.Image.GRAVITY_CENTER;

/**
 * 默认图片加载实现类
 *
 * @since 2021-03-27
 */
public class DefaultImageLoadImpl implements ImageLoad {
    private static final HiLogLabel LOG_LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00202, "DefaultImageLoadImpl");

    @Override
    public void loadImage(Image image, Object uri, LoadListener listener) {
        checkNull(image);
        checkNull(uri);
        HiLog.warn(LOG_LABEL, "No implements");
    }

    /**
     * 加载轮播图片
     *
     * @param context
     * @param path
     * @param radiusImage
     * @return Image
     */
    @Override
    public Image createImageView(Context context, String path, boolean radiusImage) {
        Image imageView = new Image(context);
        TaskDispatcher refreshUITask = context.createParallelTaskDispatcher("", TaskPriority.HIGH);
        TaskDispatcher uiTaskDispatcher = context.getUITaskDispatcher();
        refreshUITask.asyncDispatch(new Runnable() {
            @Override
            public void run() {
                HttpURLConnection connection = null;
                try {
                    URL url = new URL(path);
                    URLConnection urlConnection = url.openConnection();
                    if (urlConnection instanceof HttpURLConnection) {
                        connection = (HttpURLConnection) urlConnection;
                    }
                    if (connection != null) {
                        connection.connect();
                        InputStream inputStream = urlConnection.getInputStream();
                        ImageSource imageSource = ImageSource.create(inputStream, new ImageSource.SourceOptions());
                        ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
                        decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;
                        PixelMap pixelMap = imageSource.createPixelmap(decodingOptions);

                        uiTaskDispatcher.asyncDispatch(new Runnable() {
                            @Override
                            public void run() {
                                DirectionalLayout.LayoutConfig config = new DirectionalLayout.LayoutConfig(MATCH_PARENT, MATCH_PARENT);
                                imageView.setLayoutConfig(config);
                                if (!radiusImage) {
                                    imageView.setScaleMode(Image.ScaleMode.CLIP_CENTER);
                                    imageView.setPixelMap(pixelMap);
                                } else { // 设置圆角图片
                                    ComponentContainer.LayoutConfig layoutConfig = imageView.getLayoutConfig();
                                    int margins = AttrHelper.vp2px(15, context);
                                    layoutConfig.setMargins(margins,0,margins,0); // 设置图片边距
                                    imageView.setLayoutConfig(layoutConfig);
                                    float[] radius = new float[]{30, 30, 30, 30, 30, 30, 30, 30}; // left x 2  top x 2 right x 2 bottom x 2
                                    imageView.setPixelMap(pixelMap);
                                    Paint paint = new Paint();
                                    paint.setStyle(Paint.Style.STROKE_STYLE);
                                    paint.setStrokeWidth(10);
                                    paint.setColor(Color.RED);
                                    imageView.setClipGravity(GRAVITY_CENTER);
                                    imageView.setCornerRadii(radius);
                                    imageView.setScaleMode(Image.ScaleMode.CLIP_CENTER);
                                }
                                pixelMap.release();
                            }
                        });
                        if (inputStream != null) {
                            inputStream.close();
                        }
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                } finally {
                    if (connection != null) {
                        connection.disconnect();
                    }
                }
            }
        });
        return imageView;
    }

    private void checkNull(Object obj) {
        if (obj == null) {
            throw new NullPointerException();
        }
    }
}